<template lang="jade">
#log-in
  //- .header
    a.backup.icon-angle-left(href='javascript:history.go(-1)')
    h1.page-title 登录
  .seperate
  form.weui-cells
    .weui-cell
      .weui-cell__bd
        input.weui-input(type='text', placeholder='读者证号/身份证号', v-model='loginId')
    .weui-cell
      .weui-cell__bd
        input.weui-input(type='password', placeholder='密码', v-model='password')
  .weui-btn-area
    button.btn-blue.weui-btn.weui-btn_primary(@click.stop.prevent='login') 确定
</template>

<script>
import { mapActions } from 'vuex'
import api from '../../api-new'

export default {
  data () {
    return {
      loginId: '',
      password: ''
    }
  },
  methods: {
    ...mapActions([
      'setLoginId',
      'getUserInfo'
    ]),
    login () {
      if (this.loginId.trim().length === 0 || this.password.trim().length === 0) {
        window.alert('账号或密码不能为空')
        return
      }
      this.$weui.toast.loading()
      api.login(this.loginId, this.password)
        .then(response => {
          this.setLoginId(this.loginId)
          this.getUserInfo({
            loginId: this.loginId,
            loginType: 'RDID'
          })

          window.localStorage.setItem('loginId', this.loginId)
          // 调用绑定接口
          // const uid = this.$cookie.get('uid')
          // const wxid = this.$cookie.get('wxid')
          // api.bind(uid, this.password, wxid).then(response => {
          //   console.log(response)
          // })
        })
        .then(() => {
          this.$weui.toast.ok('登录成功')
          if (this.$route.query.fr) {
            // support full url only
            window.location.href = this.$route.query.fr
          } else {
            this.$router.replace({
              name: this.$route.params.redirect || 'home',
              params: this.$route.params.params || {}
            })
          }
        })
        .catch(err => {
          console.error(err.message)
          window.alert(err.message)
        })
    }
  }
}
</script>

<style lang='scss'>
form.weui-cells {
  border-top: none;
}
</style>
